package com.seari.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seari.bean.MenuPrivilege;
import com.seari.mapper.MenuMapper;
import com.seari.mapper.RoleMapper;
import com.seari.mapper.RolePrivilegeMapper;
import com.seari.pojo.QueryMenuPrivilege;
import com.seari.pojo.QueryRolePrivilege;
import com.seari.service.MenuService;

@Service("MenuPrivilegeService")
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private RolePrivilegeMapper rolePrivilegeMapper;
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public List<MenuPrivilege> getMenuResources(QueryMenuPrivilege qur) {
		// TODO Auto-generated method stub
		 List<MenuPrivilege> MenuPrivileges = menuMapper.getMenuResources(qur);
		 for (int i = 0; i < MenuPrivileges.size(); i++) {
			 int parentId = MenuPrivileges.get(i).getParentId();
			if (parentId != 0) {
				String parentName = roleMapper.getPmNameByParentId(parentId);
				MenuPrivileges.get(i).setParentName(parentName);
			}
		}
		 return MenuPrivileges;
	}

	@Override
	public List<MenuPrivilege> getMenuResourceByMpid(int mpid) {
		// TODO Auto-generated method stub
		return menuMapper.getMenuResourceByMpid(mpid);
	}

	@Override
	public int checkMenuPrivilegeExist(QueryMenuPrivilege qur) {
		// TODO Auto-generated method stub
		return menuMapper.checkMenuPrivilegeExist(qur);
	}

	@Override
	public int addMenuResource(MenuPrivilege menuPrivilege) {
		// TODO Auto-generated method stub
		return menuMapper.addMenuResource(menuPrivilege);
	}

	@Override
	public int updateMenuResource(MenuPrivilege menuPrivilege) {
		// TODO Auto-generated method stub
		return menuMapper.updateMenuResource(menuPrivilege);
	}

	@Override
    @Transactional(rollbackFor = Exception.class)
	public int deleteMenuResource(int mpid) {
		// TODO Auto-generated method stub
		// 删除中间表拥有该菜单的角色
		rolePrivilegeMapper.deleteRolePrivilegeByUupid(mpid);
		// 删除权限菜单
		return menuMapper.deleteMenuResource(mpid);
	}

	@Override
	public List<MenuPrivilege> menuSearch(QueryMenuPrivilege qu) {
		// TODO Auto-generated method stub
		return menuMapper.menuSearch(qu);
	}

	@Override
	public List<MenuPrivilege> getRoleMenuByUupid(int mpid) {
		// TODO Auto-generated method stub
		return menuMapper.getRoleMenuByUupid(mpid);
	}

	@Override
	public List<MenuPrivilege> getPrivilegeByMpidsAndUrid(QueryRolePrivilege qrp) {
		// TODO Auto-generated method stub
		 List<MenuPrivilege> MenuPrivileges = menuMapper.getPrivilegeByMpidsAndUrid(qrp);
		 for (int i = 0; i < MenuPrivileges.size(); i++) {
			 int parentId = MenuPrivileges.get(i).getParentId();
			if (parentId != 0) {
				String parentName = roleMapper.getPmNameByParentId(parentId);
				MenuPrivileges.get(i).setParentName(parentName);
			}
		}
		 return MenuPrivileges;
	}

	@Override
	public List<MenuPrivilege> getNotHaveByMpidsAndUrid(QueryRolePrivilege qrp) {
		// TODO Auto-generated method stub
		List<MenuPrivilege> MenuPrivileges = menuMapper.getNotHaveByMpidsAndUrid(qrp);
		 for (int i = 0; i < MenuPrivileges.size(); i++) {
			 int parentId = MenuPrivileges.get(i).getParentId();
			if (parentId != 0) {
				String parentName = roleMapper.getPmNameByParentId(parentId);
				MenuPrivileges.get(i).setParentName(parentName);
			}
		}
		 return MenuPrivileges;
	}

	@Override
	public Integer getMenuResourcesCount(QueryMenuPrivilege qu) {
		// TODO Auto-generated method stub
		return menuMapper.getMenuResourcesCount(qu);
	}

}
